package com.visionblue.business.ship.page.collection.panel.weight

import android.os.Bundle
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import com.visionblue.business.ship.R
import com.visionblue.business.ship.base.BaseFragment
import com.visionblue.business.ship.model.enums.WasteTypeEnum
import com.visionblue.business.ship.page.collection.CollectionViewModel
import com.visionblue.business.ship.page.dialog.SelectionDialog
import com.visionblue.business.ship.page.dialog.SelectionItem
import com.visionblue.business.ship.util.HighlightData
import com.visionblue.business.ship.util.TextViewUtils
import kotlinx.android.synthetic.main.frag_weight_child.*
import org.koin.androidx.viewmodel.ext.android.sharedViewModel

/**
 * @author: xlh
 * @date: 2020-05-20 5:10 PM
 * @desc:
 */
class WeightFragment : BaseFragment() {

    private val mViewModel: CollectionViewModel by sharedViewModel()

    override val layoutId: Int = R.layout.frag_weight_child

    override fun initView(savedInstanceState: Bundle?) {

        rlWeight.setOnClickListener {
            val isInCheckMode = rlWeight.tag
            if (isInCheckMode == true) return@setOnClickListener

            SelectionDialog(
                it.context,
                listOf(
                        SelectionItem(WasteTypeEnum.SOLID_WASTE.desc, action = {
                            mViewModel.recordWeight(WasteTypeEnum.SOLID_WASTE.code)
                        })

                   /*SelectionItem(WasteTypeEnum.SOLID_WASTE.desc, action = {
                        mViewModel.recordWeight(WasteTypeEnum.SOLID_WASTE.code)
                    })
                    ,
                    SelectionItem(WasteTypeEnum.OIL_PACKAGE.desc, action = {
                        mViewModel.recordWeight(WasteTypeEnum.OIL_PACKAGE.code)
                    }),
                    SelectionItem(WasteTypeEnum.WASTE_LEAD_ACID_BATTERY.desc, action = {
                        mViewModel.recordWeight(WasteTypeEnum.WASTE_LEAD_ACID_BATTERY.code)
                    })*/
                )
            ).show()
        }
    }

    override fun initData() {
        super.initData()


        mViewModel.weightLiveData.observe(this, Observer {
//            tvWeight.text = "${it}kg"
            showWeight(it)
        })

        mViewModel.inCheckMode.observe(this, Observer {
            rlWeight.tag = it
            if (it) {
                tvRecord.setTextColor(ContextCompat.getColor(context!!, R.color.greycc))
                rlWeight.setBackgroundResource(R.drawable.bg_rectround_greye6_8dp)
            } else {
                tvRecord.setTextColor(ContextCompat.getColor(context!!, R.color.black3))
                rlWeight.setBackgroundResource(R.drawable.bg_rectround_white_8dp)
            }
        })

        showWeight(0)
    }

    private fun showWeight(weight: Int) {
        tvWeight.text = TextViewUtils.highlightContent(
            context, "$weight kg",
            HighlightData(weight.toString(), R.style.textsize_60)
        )
    }

    override fun onHiddenChanged(hidden: Boolean) {
        if (!hidden) {
            mViewModel.startWatchWeightInfo()
        } else {
            mViewModel.stopWatchWeightInfo()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mViewModel.stopWatchWeightInfo()
    }

    companion object {
        fun newInstance(): WeightFragment {
            return WeightFragment()
        }
    }
}